<?php

/**
 * XHTML_Select, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Select class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Common_Element.php";
include_once "XHTML__Inline_Forms.php";
include_once "XHTML__Select_Content.php";

/**
 * The XHTML_Select class is a handler for the select xhtml-strict tag as a part
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Select extends XHTML_Common_Element implements XHTML__Inline_Forms {
    
    // xhtml attributes
        
    /**
     * Defines a unique name for the drop-down list.
     * 
     * @var string
     */
    protected $name;
    
    /**
     * Defines the number of visible items in the drop-down list.
     *
     * @var integer
     */
    protected $size;
    
    /**
     * Specifies that multiple items can be selected at a time.
     *
     * @var boolean
     */
    protected $multiple;
    
    /**
     * Disables the drop-down list.
     *
     * @var boolean
     */
    protected $disabled;
    
    /**
     * The tab index for this object.
     *
     * @var integer
     */
    protected $tabIndex;
    
    /**
     * The script for the onFocus event.
     *
     * @var string
     */
    protected $onFocusScript;
    
    /**
     * The script for the onBlur event.
     *
     * @var unknown_type
     */
    protected $onBlurScript;
    
    /**
     * The script for the onChange event.
     *
     * @var string
     */
    protected $onChangeScript;
    
    /**
     * Array of XHTML_Select_Content elements.
     *
     * @var array
     */
    protected $elements;
    
    // Methods
    
    /**
     * Constructs a XHTML_Input object.
     */
    function __construct()
    {
        settype($this->elements, "array");
    }
    
    /**
     * Sets a unique name for the drop-down list.
     *
     * @param string $name
     */
    function setName($name)
    {
        $this->name = $name;
    }
    
    /**
     * Gets a unique name for the drop-down list.
     *
     * @return string
     */
    function getName()
    {
        return $this->name;
    }
    
    /**
     * Sets the number of visible items in the drop-down list.
     *
     * @param integer $size
     */
    function setSize($size)
    {
        $this->size = $size;
    }
    
    /**
     * Gets the number of visible items in the drop-down list.
     *
     * @return integer
     */
    function getSize()
    {
        return $this->size;
    }
    
    /**
     * Specifies if multiple items can be selected at a time.
     *
     * @param boolean $multiple
     */
    function setMultiple(boolean $multiple)
    {
        $this->multiple = $multiple;
    }
    
    /**
     * Checks if multiple items can be selected at a time.
     *
     * @return boolean
     */
    function isMultiple()
    {
        return $this->multiple;
    }
    
    /**
     * Specifies if the the drop-down list is going to be disabled.
     *
     * @param boolean $disabled
     */
    function setDisabled(boolean $disabled)
    {
        $this->disabled = $disabled;
    }
    
    /**
     * Checks if the the drop-down list is going to be disabled.
     *
     * @return boolean
     */
    function isDisabled()
    {
        return $this->disabled;
    }
    
    /**
     * Sets the tab index for this object.
     *
     * @param integer $tabIndex
     */
    function setTabIndex($tabIndex)
    {
        $this->tabIndex = $tabIndex;
    }
    
    /**
     * Gets the tab index for this object.
     *
     * @return integer
     */
    function getTabIndex()
    {
        return $this->tabIndex;
    }
    
    /**
     * Sets the script for the onFocus event.
     *
     * @param string $onFocusScript
     */
    function setOnFocusScript($onFocusScript)
    {
        $this->onFocusScript = $onFocusScript;
    }
    
    /**
     * Gets the script that has been set for the onFocus event. 
     *
     * @return string
     */
    function getOnFocusScript()
    {
        return $this->onFocusScript;
    }
    
    /**
     * Sets the script for the onBlur event.
     *
     * @param string $onBlurScript
     */
    function setOnBlurScript($onBlurScript)
    {
        $this->onBlurScript = $onBlurScript;
    }
    
    /**
     * Gets the script that has been set for the onBlur event.
     *
     * @return string
     */
    function getOnBlurScript()
    {
        return $this->onBlurScript;
    }
    
    /**
     * Sets the script for the onChange event.
     *
     * @param string $onChangeScript
     */
    function setOnChangeScript($onChangeScript)
    {
        $this->onChangeScript = $onChangeScript;
    }
    
    /**
     * Gets the script that has been set for the onChange event.
     *
     * @return string
     */
    function getOnChangeScript()
    {
        return $this->onChangeScript;
    }
    
    /**
     * Sets the XHTML__Select_Content elements array.
     *
     * @param array
     */
    function setElements($elements) {
        $this->elements = $elements;
    }
    
    /**
     * Gets the XHTML__Select_Content elements array.
     *
     * @return array
     */
    function getElements()
    {
        return $this->elements;
    }
    
    /**
     * Adds an XHTML__Select_Content object to the inner elements.
     *
     * @param XHTML__Select_Content $element
     */
    function addElement(XHTML__Select_Content $element)
    {
        $this->elements[] = $element;
    }
    
    /**
     * Removes the specified XHTML__Select_Content object from the inner 
     * elements.
     *
     * @param XHTML__Select_Content $element
     */
    function removeElement(XHTML__Select_Content $element)
    {
        if (($res = array_search($option, $this->elements)) !== false) {
            unset($this->elements[$res]);
        }
    }


    // Overriding the toString method for a proper implementation.
    
    /**
    * Returns the string representation for echoing the class atributes.
    *
    * @return string
    */
    function toString()
    {
        $auxString = "<select".parent::toString();
        
        /*
         * Addition of the object own attributes to the return string.
         */
        
        if (isset($this->name)) {
            $auxString = $auxString."name=\"".$this->name."\" ";
        }
        
        if (isset($this->size)) {
            $auxString = $auxString."size=\"".$this->size."\" ";
        }
        
        if ($this->multiple) {
            $auxString = $auxString."multiple=\"multiple\" ";
        }
        
        if ($this->disabled) {
            $auxString = $auxString."disabled=\"disabled\" ";
        }
        
        if (isset($this->tabIndex)) {
            $auxString = $auxString."tabindex=\"".$this->tabIndex."\" ";
        }
        
        if (isset($this->onFocusScript)) {
            $auxString = $auxString."onfocus=\"".$this->onFocusScript."\" ";
        }
        
        if (isset($this->onBlurScript)) {
            $auxString = $auxString."onblur=\"".$this->onBlurScript."\" ";
        }
        
        if (isset($this->onChangeScript)) {
            $auxString = $auxString."onchange=\"".$this->onChangeScript."\" ";
        }
        
        $auxString = $auxString.">\n";
        
        /*
         * Addition of the inner elements of the select tag.
         */
        if (count($this->elements) > 0) {
            foreach ($this->elements as $element) {
                $auxString = $auxString.$element->toString();
            }
        }
        
        $auxString = $auxString."</select>\n";
        
        return $auxString;
    }
    
}

?>